package View;

import DB.Evento.ControllerEvento;
import DB.Inscricao.ControllerInscricao;
import Model.Evento;
import Model.Usuario;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.ComboBoxModel;
import javax.swing.JOptionPane;

public class InscricaoAddNewFromUser extends javax.swing.JFrame {

    Usuario u;

    public InscricaoAddNewFromUser(Usuario u) {
        this.u = u;
        initComponents();

        /**
         * Carregando ComboBox de eventos
         */
        ComboBoxModel m = comboEventos.getModel();
        ControllerEvento ce = ControllerEvento.getInstance();

        ArrayList<Evento> eventos = ce.getOpenEventos();

        for (Evento ev : eventos) {
            comboEventos.addItem(ev);
        }

        /**
         * Carregando ComboBox de usuarios
         */
        ComboBoxModel cbm = comboUsuarios.getModel();

        comboUsuarios.addItem(u);
    }

    public void fazInscricao() {
        ControllerInscricao ci = ControllerInscricao.getInstance();
        Evento e = (Evento) comboEventos.getSelectedItem();
        Usuario u = (Usuario) comboUsuarios.getSelectedItem();

        long result = -1;

        try {
            result = ci.inscricao(e, u);
        } catch (SQLException ex) {
            if (ex.getMessage().equals("[SQLITE_CONSTRAINT]  Abort due to constraint violation (columns idEvento, idParticipante are not unique)")) {
                JOptionPane.showMessageDialog(null, "Usuário já inscrito nesse evento.");
            } else if (ex.getMessage().equals("Limite de vagas atingido.")) {
                JOptionPane.showMessageDialog(null, "Não há mais vagas nesse evento.");
            }
        }

            if (result > 0) {
                JOptionPane.showMessageDialog(null, "Inscrição feita." + '\n' + "Gerando boleto de cobrança...");
                this.setVisible(false);
            } else {
                JOptionPane.showMessageDialog(null, "Ocorreu algum erro durante a inscrição.");
            }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        comboEventos = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        comboUsuarios = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setTitle("INSCRIÇÃO");
        setResizable(false);

        jButton1.setText("Inscrever");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Selecione o evento em que será feita a inscrição:");

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Selecione o participante que será inscrito:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(comboEventos, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(comboUsuarios, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                .addGap(221, 221, 221)
                .addComponent(jButton1)
                .addContainerGap(220, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(comboEventos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(comboUsuarios, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(26, Short.MAX_VALUE))
        );

        setSize(new java.awt.Dimension(536, 228));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        fazInscricao();
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox comboEventos;
    private javax.swing.JComboBox comboUsuarios;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration//GEN-END:variables
}
